package cn.kxx.service.impl;

import cn.kxx.mapper.UserMapper;
import cn.kxx.pojo.Role;
import cn.kxx.pojo.UserInfo;
import cn.kxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        UserInfo userInfo = userMapper.findUserByUserName(s);
        //把查询到的用户名 密码 封装到安全框架的user对象中
        User user = new User(
                userInfo.getUsername(),
                userInfo.getPassword(),
                userInfo.getStatus() == 0 ? false : true ,//用户是否开启 0 表示未开启，1表示开启
                true,  //帐号是否过期  不过期
                true, //证书  不过期
                true,// 帐号 不锁定
                getAuthority(userInfo.getRoles()));

        return user;
    }

    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        for (Role role : roles) {
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }

        return  simpleGrantedAuthorities;
    }




    @Autowired
    private PasswordEncoder passwordEncoder;
    //查询所有user信息
    @Override
    public List<UserInfo> findAll() {
      List<UserInfo> list =  userMapper.findAll();
      return list;
    }


        // <!--新增user信息-->

    @Override
    public void save(UserInfo userInfo) {

        String encode = passwordEncoder.encode(userInfo.getPassword());
        userInfo.setPassword(encode);


        userMapper.save(userInfo);

    }

    @Override
    public UserInfo findById(Integer id) {
        UserInfo userInfo = userMapper.findById(id);
        return userInfo;
    }


    //查询用户没有的权限
    @Override
    public List<Role> findUserByIdAndAllRole(Integer id) {
      List<Role> list =  userMapper.findUserByIdAndAllRole(id);
      return list;
    }

    //添加权限
    @Override
    public void addRoleToUser(Integer userId,Integer[] ids) {

        for (Integer id : ids) {

            userMapper.addRoleToUser(userId,id);

        }
    }

    /*删除单个*/
    @Override
    public void deleteUser(int id) {

        userMapper.deleteUser(id);
        userMapper.deleteUser1(id);

    }


    //选中删除
    @Override
    public void selectDelete(String[] ids) {

        for (String id : ids) {

            userMapper.deleteUser(Integer.parseInt(id));
            userMapper.deleteUser1(Integer.parseInt(id));

        }

    }


}
